package com.conferenceroom.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.conferenceroom.dto.AjaxResult;
import com.conferenceroom.dto.CustomerReport;
import com.conferenceroom.entity.Record;
import com.conferenceroom.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/records")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @GetMapping
    public AjaxResult getAllRecords() {
        List<Record> records = recordService.list();
        if (records == null || records.isEmpty()) {
            return AjaxResult.fail("未查询到维修记录！");
        }
        return AjaxResult.success(records);
    }

    @GetMapping("/page")
    public AjaxResult getAllRecordsByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Record> page = new Page<>(pageNum, pageSize);
        Page<Record> recordPage = recordService.page(page);
        return AjaxResult.success(recordPage);
    }

    @GetMapping("/{id}")
    public AjaxResult getRecordById(@PathVariable Integer id) {
        Record record = recordService.getById(id);
        if (record != null) {
            return AjaxResult.success(record);
        } else {
            return AjaxResult.fail("不存在该维修记录！");
        }
    }

    @PostMapping
    public AjaxResult saveRecord(@RequestBody Record record) {


            boolean saved = recordService.save(record);
            if (saved) {
                return AjaxResult.success("新增维修记录成功！");
            } else {
                return AjaxResult.fail("新增失败！");
            }


    }

    @PutMapping("/{id}")
    public AjaxResult updateRecord(@PathVariable Integer id, @RequestBody Record record) {
        record.setMaintenanceId(id);
        record.setStartTime(new Date(record.getStartTime().getTime() + 8 * 60 * 60 * 1000L));
            try{
                boolean updated = recordService.updateById(record);

                if (updated) {
                    return AjaxResult.success("更新维修记录成功！");
                } else {
                    return AjaxResult.fail("更新失败，请检查数据！");
                }
            }catch ( Exception e){
                 return AjaxResult.fail("更新失败:"+e.getMessage());
            }


    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteRecordById(@PathVariable Integer id) {

            boolean deleted = recordService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除维修记录成功！");
            } else {
                return AjaxResult.fail("删除失败，记录不存在！");
            }

    }

    @GetMapping("/user/{userId}")
    public AjaxResult getRecordsByUserId(@PathVariable Integer userId) {
        List<Record> records = recordService.getRecordsByUserId(userId);
        if (records == null || records.isEmpty()) {
            return AjaxResult.fail("该用户没有维修记录！");
        }
        return AjaxResult.success(records);
    }

    @GetMapping("/room/{roomId}")
    public AjaxResult getRecordsByRoomId(@PathVariable Integer roomId) {
        List<Record> records = recordService.getRecordsByRoomId(roomId);
        if (records == null || records.isEmpty()) {
            return AjaxResult.fail("该会议室没有维修记录！");
        }
        return AjaxResult.success(records);
    }

    @PostMapping("/page/condition")
    public AjaxResult pageByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody Record record) {

        Page<Record> page = new Page<>(pageNum, pageSize);
        IPage<Record> result = recordService.pageByCondition(page, record);

        return AjaxResult.success(result);
    }


    /**
     * 客户提交报修问题
     */
    @PostMapping("/report")
    public AjaxResult reportProblem(@RequestBody CustomerReport customerReport) {

        AjaxResult result = recordService.customerReport(customerReport);
        return result;
    }


}
